using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Super
{
	public class UISlope : BaseMeshEffect
	{
		public enum SlopeType
		{
			Left,
			Right,
			LeftAndRight
		}
		
		[Range(-80, 80)]
		public float slopeAngle = 0;
		public SlopeType slopeType = SlopeType.Right;
		
		public override void ModifyMesh(VertexHelper helper)
		{
			UIVertex v = new UIVertex();
			for (int i = 0; i < helper.currentVertCount; i++)
			{
				helper.PopulateUIVertex(ref v, i);
				AmendVertexPos(ref v, i);
				helper.SetUIVertex(v, i);
			}
		}
		
		public void AmendVertexPos(ref UIVertex v, int index)
		{
			if (index == 0)
			{
				if (slopeAngle >= 0) return;
				if (slopeType == SlopeType.Right) return;
				v.position += new Vector3(GetOffsetX(), 0, 0);
				return;
			}
			
			if (index == 1)
			{
				if (slopeAngle <= 0) return;
				if (slopeType == SlopeType.Right) return;
				v.position += new Vector3(GetOffsetX(), 0, 0);
				return;
			}
			
			if (index == 2)
			{
				if (slopeAngle <= 0) return;
				if (slopeType == SlopeType.Left) return;
				v.position += new Vector3(GetOffsetX(), 0, 0);
				return;
			}
			
			if (index == 3)
			{
				if (slopeAngle >= 0) return;
				if (slopeType == SlopeType.Left) return;
				v.position += new Vector3(GetOffsetX(), 0, 0);
				return;
			}
			
		}
		
		public float GetOffsetX()
		{
			float height = GetComponent<RectTransform>().rect.height;
			float offsetX = height * Mathf.Tan(slopeAngle * Mathf.Deg2Rad);
			return MathF.Abs(offsetX);
		}
	}
}